package com.plateno.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.plateno.bean.model.Config.MsgCode;
import com.plateno.bean.req.SensitiveWordReq;
import com.plateno.bean.resp.BaseResp;
import com.plateno.service.SensitiveWordInitService;
import com.plateno.service.SensitiveWordService;
import com.plateno.service.SensitivewordEngineService;
import com.plateno.util.JsonUtil;

@RestController
@RequestMapping("/sensitiveWord")
public class SensitiveWordController {

	private static final Logger logger = LoggerFactory.getLogger(SensitiveWordService.class);
	@Resource
	private SensitiveWordService sensitiveWordService;
	@Autowired
	private SensitiveWordInitService sensitiveWordInitService;

	// 是否包含敏感词
	@RequestMapping(value = "/contain", method = RequestMethod.POST)
	public String containSensitiveWord(HttpServletRequest request, @RequestBody SensitiveWordReq req)
			throws IOException {
		boolean flag = SensitivewordEngineService.isContaintSensitiveWord(req.getSource(), 2);
		return JsonUtil.generateSuccessResponse(flag);
	}

	// 返回敏感词
	@RequestMapping(value = "/getByIK", method = RequestMethod.POST)
	public String getSw1(HttpServletRequest request, @RequestBody SensitiveWordReq req) throws IOException {
		BaseResp resp = new BaseResp();
		resp.setResult(sensitiveWordService.wordFilterByIK(req.getSource()));
		return JsonUtil.generateSuccessResponse(resp);
	}

	@RequestMapping(value = "/getByDFA", method = RequestMethod.POST)
	public String getSw2(HttpServletRequest request, @RequestBody SensitiveWordReq req) {
		BaseResp resp = new BaseResp();
//		long st = System.currentTimeMillis();
		resp.setResult(sensitiveWordService.wordFilterByDFA(req.getSource()));
//		System.out.println("DFA过滤词耗时" + (System.currentTimeMillis() - st));
		try {
			return JsonUtil.toJsonString(resp);
		} catch (IOException e) {
			logger.error("getByDFA json 转换错误", e);
			return JsonUtil.generateResponse(MsgCode.SYSTEM_ERROR);
		}
		
	}

	// 敏感词替换
	@RequestMapping(value = "/replace", method = RequestMethod.POST)
	public String replaceSW(HttpServletRequest request, @RequestBody SensitiveWordReq req) {
		BaseResp resp = new BaseResp();
		resp.setResult(sensitiveWordService.wordFilterByDFA(req.getSource()));
		return SensitivewordEngineService.replaceSensitiveWord(req.getSource(), 2,
				StringUtils.isEmpty(req.getRelapce()) ? "***" : req.getRelapce());
	}

	// 敏感词替换
	@RequestMapping(value = "/load", method = RequestMethod.GET)
	public String load(HttpServletRequest request, @RequestBody SensitiveWordReq req) {
		try {
			sensitiveWordInitService.loadKeyWord();
		} catch (Exception e) {
			logger.error("load敏感词错误", e);
			return JsonUtil.generateResponse(MsgCode.SYSTEM_ERROR);
		}
		
		return JsonUtil.generateSuccessResponse("suc");
	}
}
